package com.ziyun.erp.web.api.goods;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.utility.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.goods.entity.GoodsAttributeEntity;
import com.ziyun.erp.modules.goods.service.GoodsAttributeService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;

/**
 * 商品属性表
 *
 * @author xueqz
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("port/goods/goodsattribute")
public class GoodsAttributeControllerApi {
	@Autowired
	private GoodsAttributeService goodsAttributeService;
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	@Autowired
	private GoodsOptionService goodsOptionService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = goodsAttributeService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{attributeId}")
	public R info(@PathVariable("attributeId") Long attributeId) {
		GoodsAttributeEntity goodsAttribute = goodsAttributeService.selectById(attributeId);
		GoodsCategoryEntity goodsCategory = goodsCategoryService.selectById(goodsAttribute.getCategoryId());
		return R.ok().put("goodsAttribute", goodsAttribute).put("goodsCategory", goodsCategory);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody GoodsAttributeEntity goodsAttribute) {
		goodsAttributeService.saveAttriute(goodsAttribute);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody GoodsAttributeEntity goodsAttribute) {
		ValidatorUtils.validateEntity(goodsAttribute);
		goodsAttributeService.updateAllColumnById(goodsAttribute);// 全部更新
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] attributeIds) {
		// goodsAttributeService.deleteBatchIds(Arrays.asList(attributeIds));
		// 标记删除规格属性
		goodsAttributeService.fagDelAttribute(Arrays.asList(attributeIds));
		return R.ok();
	}

	/**
	 * 获得所有的规格属性,供商品模板选择规格属性使用
	 */
	@RequestMapping("/getTempGoodsAttribute/{categoryId}")
	public R getTempGoodsAttribute(@PathVariable("categoryId") Integer categoryId) {
		List<GoodsAttributeEntity> attributeEntityList = goodsAttributeService.getTemplateAttribute(categoryId);
		return R.ok().put("attributeEntityList", attributeEntityList);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/getAttributesByTemplateId")
	public R getAttributesByTemplateId(@RequestBody Map paramBodyMap) {
		Map param = (Map) paramBodyMap.get("params");
		Integer templateId = (Integer) param.get("templateId");
		Long sellerId = Long.valueOf(String.valueOf(param.get("sellerId")));
//		List<GoodsAttributeEntity> attributeList = goodsAttributeService.getAttributesByTemplateId(templateId);
//		List<GoodsAttributeEntity> craftAttributeList = goodsAttributeService
//				.getCraftAttributesByTemplateId(templateId);
		return goodsAttributeService.getAttributesByTemplateId(templateId,sellerId);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/getCraftAttributesByTemplateId")
	public R getCraftAttributesByTemplateId(@RequestBody Map paramBodyMap) {
		Integer templateId = (Integer) paramBodyMap.get("templateId");
		List<GoodsAttributeEntity> craftAttributeList = goodsAttributeService
				.getCraftAttributesByTemplateId(templateId);
		return R.ok().put("craftAttributeList", craftAttributeList);
	}

	/**
	 * 生成私有选项
	 */
	@RequestMapping("/newOption")
	public R newOption(@RequestBody Map paramBodyMap){
		Map params = (Map) paramBodyMap.get("params");
		String goodsOptionEntity = JsonUtils.object2String(params.get("goodsOptionEntity"));
		GoodsOptionEntity goodsOptionEntity1 = JsonUtils.string2Object(goodsOptionEntity, GoodsOptionEntity.class);
		goodsOptionService.insert(goodsOptionEntity1);
		return R.ok().put("goodsOptionEntity",goodsOptionEntity1);
	}

}
